#include <list>
#include <string>
#include <iostream>
// 观察者基类
class Observer {
public:
    virtual void update(float price) = 0; // 纯虚函数，用于更新通知
};

// 主题基类
class Subject {
public:
    virtual void attach(Observer* observer) = 0;
    virtual void detach(Observer* observer) = 0;
    virtual void notify() = 0;
};
class StockPrice : public Subject {
private:
    std::list<Observer*> observers;
    float price;

public:
    StockPrice() : price(0.0f) {}

    void attach(Observer* observer) override {
        observers.push_back(observer);
    }

    void detach(Observer* observer) override {
        observers.remove(observer);
    }

    void notify() override {
        for (Observer* observer : observers) {
            observer->update(price);
        }
    }

    void setPrice(float newPrice) {
        if (price != newPrice) {
            price = newPrice;
            notify();  // 通知所有观察者价格变动
        }
    }
};
// 仪表板观察者
class DashboardDisplay : public Observer {
public:
    void update(float price) override {
        std::cout << "Dashboard display updated: Stock price is now $" << price << std::endl;
    }
};

// 风险管理观察者
class RiskManagement : public Observer {
public:
    void update(float price) override {
        if (price > 100.0f) {
            std::cout << "Risk Management Alert: Stock price above $100 - $" << price << std::endl;
        }
    }
};

int main() {
    StockPrice stockPrice;  						// 创建主题对象
    DashboardDisplay dashboard;  					// 创建具体观察者
    RiskManagement riskManagement;  				// 创建另一个具体观察者

    stockPrice.attach(&dashboard);  				// 注册观察者
    stockPrice.attach(&riskManagement);  			// 注册另一个观察者

    stockPrice.setPrice(95.0f);  					// 更新价格，通知观察者
    stockPrice.setPrice(105.0f);  					// 再次更新价格，通知观察者

    return 0;
}